﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FTools.Dominio;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using FTools.Generico;

namespace FTools.Persistencia
{
    class PerDepartamento
    {
        static string msg = "";

        //OBTENER DEPARTAMENTO
        public Departamento obtenerDepartamento(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdDepartamento)
        {
            DataTable dataTable = new DataTable();
            PerPais objPerPais = new PerPais();
            try
            {
                SqlCommand command = new SqlCommand("select * from DEPARTAMENTO where ID_DEPARTAMENTO=@IdDepartamento");

                command.Parameters.AddWithValue("@IdDepartamento", xIdDepartamento);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                Departamento objDepartamento = new Departamento();
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    Pais objPais = objPerPais.obtenerPais(xConexion, xTransaccion, (long)fila.ItemArray[2]);

                    objDepartamento.IdDepartamento = (long)fila.ItemArray[0];
                    objDepartamento.Nombre = (String)fila.ItemArray[1]; objDepartamento.Nombre = objDepartamento.Nombre.Trim();
                    objDepartamento.ObjPais = objPais;
                }
                return objDepartamento;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE DEPARTAMENTO
        public Boolean existeDepartamento(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdDepartamento)
        {
            DataTable dataTable = new DataTable();
            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from DEPARTAMENTO where ID_DEPARTAMENTO=@IdDepartamento");

                command.Parameters.AddWithValue("@IdDepartamento", xIdDepartamento);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //EXISTE DEPARTAMENTO
        public Boolean existeDepartamento(SqlConnection xConexion, SqlTransaction xTransaccion, String xNombre, long xIdPais)
        {
            DataTable dataTable = new DataTable();
            try
            {
                SqlCommand command = new SqlCommand("select count(*) as EXISTE from DEPARTAMENTO where NOMBRE=@Nombre and ID_PAIS=@IdPais");

                command.Parameters.AddWithValue("@Nombre", xNombre.Trim());
                command.Parameters.AddWithValue("@IdPais", xIdPais);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                if (dataTable.Rows.Count >= 0)
                {
                    DataRow fila = dataTable.Rows[0];
                    if ((int)fila.ItemArray[0] > 0)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //OBTENER COLECCION DEPARTAMENTO (POR PAIS)
        public ArrayList obtenerColeccionDepartamento(SqlConnection xConexion, SqlTransaction xTransaccion, long xIdPais)
        {
            DataTable dataTable = new DataTable();
            SqlDataAdapter dataAdapter;
            ArrayList colDepartamento = new ArrayList();
            PerPais objPerPais = new PerPais();
            try
            {
                SqlCommand command = new SqlCommand("select * from DEPARTAMENTO where ID_PAIS=@IdPais");
                command.Parameters.AddWithValue("@IdPais", xIdPais);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                dataAdapter = new SqlDataAdapter(command);
                dataAdapter.Fill(dataTable);
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    Departamento objDepartamento = new Departamento();
                    DataRow fila = dataTable.Rows[i];
                    Pais objPais = objPerPais.obtenerPais(xConexion, xTransaccion, (long)fila.ItemArray[2]);

                    objDepartamento.IdDepartamento = (long)fila.ItemArray[0];
                    objDepartamento.Nombre = (String)fila.ItemArray[1]; objDepartamento.Nombre = objDepartamento.Nombre.Trim();
                    objDepartamento.ObjPais = objPais;
                    colDepartamento.Add(objDepartamento);
                }
                return colDepartamento;
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //ALTA DEPARTAMENTO
        public int altaDepartamento(SqlConnection xConexion, SqlTransaction xTransaccion, Departamento xObjDepartamento)
        {
            try
            {
                SqlCommand command = new SqlCommand("insert into DEPARTAMENTO values (@IdDepartamento, @Nombre, @IdPais)");

                command.Parameters.AddWithValue("@IdDepartamento", xObjDepartamento.IdDepartamento);
                command.Parameters.AddWithValue("@Nombre", xObjDepartamento.Nombre.Trim());
                command.Parameters.AddWithValue("@IdPais", xObjDepartamento.ObjPais.IdPais);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //BAJA DEPARTAMENTO
        public int bajaDepartamento(SqlConnection xConexion, SqlTransaction xTransaccion, Departamento xObjDepartamento)
        {
            try
            {
                SqlCommand command = new SqlCommand("delete from DEPARTAMENTO where ID_DEPARTAMENTO=@IdDepartamento");

                command.Parameters.AddWithValue("@IdDepartamento", xObjDepartamento.IdDepartamento);
                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }

        //MODIFICA DEPARTAMENTO
        public int modificaDepartamento(SqlConnection xConexion, SqlTransaction xTransaccion, Departamento xObjDepartamento)
        {
            try
            {
                SqlCommand command = new SqlCommand("update DEPARTAMENTO set NOMBRE=@Nombre, ID_PAIS=@IdPais " +
                                                    "where ID_DEPARTAMENTO = @IdDepartamento");

                command.Parameters.AddWithValue("@IdDepartamento", xObjDepartamento.IdDepartamento);
                command.Parameters.AddWithValue("@Nombre", xObjDepartamento.Nombre.Trim());
                command.Parameters.AddWithValue("@IdPais", xObjDepartamento.ObjPais.IdPais);

                command.Connection = xConexion;
                command.Transaction = xTransaccion;
                return command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    msg += "Error #" + i + " Message: " + ex.Errors[i].Message + "\n";
                }
                System.Console.WriteLine(msg);
                throw new Exception("#Persistencia# " + ex.Message);
            }
        }
    }
}
